package cn.wawi.common.proxy;

import java.lang.reflect.Method;
import cn.wawi.entity.sys.User;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * @author : 龚亮
 * @Date : 2015-06-27 上午9:14:15
 * @Comments : cglib动态代理工具类
 * @Version : 1.0.0
 */
public class ProxyUtil implements MethodInterceptor{

	protected Advice advice;
	public ProxyUtil(){
		//this.advice=new MyAdvice();
	}
    public Object getProxy(Class<?> clazz) {
    	Enhancer enhancer = new Enhancer();
        //设置需要创建的子类
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        //通过字节码技术动态创建子类实例
        return enhancer.create();
    }
	//动态代理
	public Object intercept(Object obj, Method method, Object[] args,MethodProxy proxy) throws Throwable {
	//	advice.before(obj, method, args, proxy);
		Object result = proxy.invokeSuper(obj, args);
	//	advice.after(obj, method, args, proxy);
		return result;
	}

	public static void main(String[] args) {
		ProxyUtil proxy = new ProxyUtil();
        //通过动态生成子类的方式创建代理类
		User target = (User) proxy.getProxy(User.class);
		target.setEmail("a");
	}
	
}
